#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include"Logger.hpp"

const static std::string sep=" ";

class Dict
{
private:
    void LoadDict()
    {
        std::ifstream in(_path);
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR)<<"打开字典文件失败"<<_path;
            return;
        }
        std::string line;
        while(std::getline(in,line))
        {
            auto pos=line.find(sep);
            if(pos==std::string::npos)
            {
                LOG(LogLevel::WARNING)<<"查找错误";
                continue;
            }
            std::string world=line.substr(0,pos);
            std::string value=line.substr(pos+sep.size());
            if(world.empty()||value.empty())
            {
                continue;
            }
            _dict.insert(std::make_pair(world,value));
        }

        in.close();
    }

public:
    Dict(std::string &path)
        : _path(path)
    {
        LoadDict();
    }
    std::string Translate(const std::string &world)
    {
        auto it=_dict.find(world);
        if(it==_dict.end())
        {
            return "暂时还未收录该单词";
        }
        return it->first+"的意思是："+it->second;
    }
    ~Dict() {}

private:
    std::string _path;
    std::unordered_map<std::string, std::string> _dict;
};